package com.connectandroid;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

/**
 * Created by JFJ on 2017-11-3.
 */

public class MyNativeModule extends ReactContextBaseJavaModule{

    private Context mContext;

    public MyNativeModule(ReactApplicationContext reactContext) {
        super(reactContext);
        mContext = reactContext;
    }

    @Override
    public String getName() {
        return "MyNativeModule";
    }

    @ReactMethod
    public void rnCallNativeToast(String msg){
        Toast.makeText(mContext,msg,Toast.LENGTH_SHORT).show();
    }

    @ReactMethod
    public void rnCallNativeDialog(String msg){
    /* @setIcon 设置对话框图标
     * @setTitle 设置对话框标题
     * @setMessage 设置对话框消息提示
     * setXXX方法返回Dialog对象，因此可以链式设置属性
     */
    final AlertDialog.Builder normalDialog = new AlertDialog.Builder(getCurrentActivity());
    normalDialog.setTitle("我是一个普通Dialog");
    normalDialog.setMessage("你要点击哪一个按钮呢?");
    normalDialog.setPositiveButton("确定",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //...To-do
                    WritableMap writableMap = new WritableNativeMap();
                    writableMap.putString("key", "yes");
                    sendTransMisson((ReactContext) mContext, "getResultFromAndroid1", writableMap);
                }
            });
    normalDialog.setNegativeButton("关闭",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //...To-do
                    WritableMap writableMap = new WritableNativeMap();
                    writableMap.putString("key", "no");
                    sendTransMisson((ReactContext) mContext, "getResultFromAndroid2", writableMap);
                }
            });
    // 显示
    normalDialog.show();
    }
    /**
     * @param reactContext
     * @param eventName    事件名
     * @param params       传惨
     */
    public void sendTransMisson(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, params);

    }

    @ReactMethod
    public void  callBackTime(String name,Callback callback){
        callback.invoke("callback返回值:"+name);
    }

    @ReactMethod
    public void sendPromiseTime(String name, Promise promise) {
        WritableMap writableMap=new WritableNativeMap();
        writableMap.putString("name",name);
        writableMap.putString("age","20");
        writableMap.putString("time","2017-10-10 12:59:59");
        promise.resolve(writableMap);
    }

}
